<template>
  <el-form ref="formobj" :model="wyform" :rules="rules" label-width="100px">
    <el-form-item label="号别编码" prop="registCode">
      <el-input v-model="wyform.registCode" maxlength="64" placeholder="请输入号别编码"></el-input>
    </el-form-item>
    <el-form-item label="号别名称" prop="registName">
      <el-input v-model="wyform.registName" maxlength="64" placeholder="请输入号别名称"></el-input>
    </el-form-item>
    <el-form-item label="挂号费" prop="registFee">
      <el-input v-model="wyform.registFee" maxlength="64" placeholder="请输入挂号费"></el-input>
    </el-form-item>
    <el-form-item label="挂号限额" prop="registQuota">
      <el-input v-model.number="wyform.registQuota" maxlength="64" placeholder="请输入挂号限额"></el-input>
    </el-form-item>
    <el-form-item label="显示序列号" prop="sequenceNo">
      <el-input v-model.number="wyform.sequenceNo" maxlength="64" placeholder="请输入序列号"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save" v-if="props.id">修改</el-button>
      <el-button type="primary" @click="save" v-else>保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, ref,defineEmits } from 'vue'
import { post,get } from '@/util/axios'
import {url} from './api.js'
const wyform = reactive({
  registCode: '',
  registName: '',
  registFee: '',
  registQuota:'',
  sequenceNo:''
})
const emits=defineEmits(['update:show','getTableData'])
const props=defineProps(['id'])

const check=(rule,value,callback,field)=>{
  get(url.check,{field,value,id:props.id},content=>{
    if (content){
      callback()
    }else{
      callback(new Error())
    }
  })

}
const rules = reactive({
  registCode: [
    { required: true, message: '请输入号别编码', trigger: 'blur' },
    { validator:(rule,value,callback)=>{check(rule,value,callback,'regist_code')},message: "编码已存在"}
  ],
  registName: [
    { required: true, message: '请输入号别名称', trigger: 'blur' },
    { validator:(rule,value,callback)=>{check(rule,value,callback,'regist_name')},message: "名称已存在"}
  ],
  registFee: [
    { required: true, message: '挂号费', trigger: 'blur' },
    { pattern: /^[0-9]+(\.[0-9]{1,2})?$/, message: '请输入正确的金额', trigger: 'blur' }
  ],
  registQuota: [
    { required: true, message: '挂号限额', trigger: 'blur' },
    { type: 'integer', message: '只能输入整数', trigger: 'blur' }
  ],
  sequenceNo: [
    { required: true, message: '显示序列号', trigger: 'blur' },
    { type: 'integer', message: '只能输入整数', trigger: 'blur' }
  ]
})
const formobj = ref()
if (props.id){
  getById()
}

function getById(){

  get(url.getById, {id:props.id}, content => {
        for(const key in wyform){
          if (Object.prototype.hasOwnProperty.hasOwnProperty.call(content,key)){
            wyform[key]=content[key]
          }
        }
    wyform.id=props.id
    wyform.registFee=wyform.registFee/100
  }, formobj)
}

function save () {
    wyform.registFee=wyform.registFee*100
    post(props.id?url.update :url.add, wyform, content => {
      console.log(wyform)
      emits('update:show',false),
          emits('getTableData')
    }, formobj)
}
</script>

<style scoped lang="scss">
</style>
